package m202401.d08;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/8 10:02
 */
//101. 对称二叉树
public class Q101 {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (root.left != null && root.right != null) {
            return isSymmetric(root.left, root.right);
        }
        if (root.left == null && root.right == null) {
            return true;
        }
        return false;
    }

    public boolean isSymmetric(TreeNode p, TreeNode q) {
        if (!isSameNode(p, q)) {
            return false;
        }
        if (p == null && q == null) {
            return true;
        }
        return isSymmetric(p.left, q.right) && isSymmetric(p.right, q.left);
    }

    public boolean isSameNode(TreeNode p, TreeNode q) {
        if (p != null && q != null) {
            return p.val == q.val;
        }
        return p == null && q == null;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
